package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import dao.Connector;
import java.util.List;
 import constants.Constants;
import java.util.logging.Level;
import java.util.logging.Logger;
public class InfoBooking {

	Connection connect =null;
	PreparedStatement pstm=null;
	private String table="tblInfoBook";
	public boolean addInfoBooking(int id_customer,String date_rg,int number,int total,int payment,String checkin,String checkout,int status){
		if(payment>total) payment=total;
                connect = Connector.getConnect();
		try{
			pstm= connect.prepareStatement("INSERT INTO "+this.table+"(id_customer,date_rg,total,number,payment"
                                + ",date_checkin,date_checkout,status) VALUES (?,?,?,?,?,?,?,?)");
			pstm.setInt(1,id_customer);
			pstm.setString(2,date_rg);                       
			pstm.setInt(3,total);
			pstm.setInt(4,number);
			pstm.setInt(5,payment);
                        pstm.setString(6, checkin);
                        pstm.setString(7, checkout);
                        pstm.setInt(8,status);
			if(pstm.executeUpdate()>0){
				return true;
			}
			
		}catch(SQLException e){
			e.printStackTrace();
		}
		return false;
		
	}
	public boolean deleteInfoBooking(int id){
		connect = Connector.getConnect();
		try{
			pstm = connect.prepareStatement("DELETE FROM "+this.table+ " WHERE id_customer = ?");
			pstm.setInt(1, id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			
		}
		return false;
	}
	public boolean updateInfoBooking(int id_customer,String date_rg,int number,int total,int payment,String checkin,String checkout,int status){
		if(payment>total) payment=total;
                connect= Connector.getConnect();
		try{
			pstm = connect.prepareStatement("UPDATE "+this.table +" SET date_rg=?,number=?,total=?,"
                                + "payment=?,date_checkin=?,date_checkout=?,status=?  WHERE id_customer=?");
			pstm.setInt(8,id_customer);
			pstm.setString(1,date_rg);
			pstm.setInt(2,number);
			pstm.setInt(3,total);
			pstm.setInt(4,payment);
                        pstm.setString(5,checkin);
                        pstm.setString(6,checkout);
                        pstm.setInt(7, status);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		return false;
	}
        public boolean updateStatus(int id_customer,int status){
       connect= Connector.getConnect();
		try{
			pstm = connect.prepareStatement("UPDATE "+this.table +" SET status=?  WHERE id_customer=?");
			pstm.setInt(2,id_customer);
                        pstm.setInt(1, status);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		return false;
        }
	public ArrayList<entity.InfoBooking> getAllInfoBooking(){
		connect= Connector.getConnect();
		ArrayList<entity.InfoBooking> list = new ArrayList<entity.InfoBooking>();
		ResultSet rs=null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table);
			rs=pstm.executeQuery();
			while(rs.next()){
				entity.InfoBooking ifb = new entity.InfoBooking();
				ifb.setId(rs.getInt("id"));
				ifb.setId_customer(rs.getInt("id_customer"));
				ifb.setDate(rs.getString("date_gr"));
				ifb.setNumber_room(rs.getInt("number"));
				ifb.setTotal(rs.getInt("total"));
				ifb.setPayment(rs.getInt("payment"));
                                ifb.setCheckin(rs.getString("date_checkin"));
                                ifb.setCheckout(rs.getString("date_checkout"));
                                ifb.setStatus(rs.getInt("status")); 
				list.add(ifb);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		return list;
	}
	
        public List<entity.InfoBooking> getInfoBookingByStatus(int status){
		connect= Connector.getConnect();
		ResultSet rs=null;
		List<entity.InfoBooking> list = new ArrayList<>();
		try{

			pstm= connect.prepareStatement("Select *from "+this.table+" where status="+status);
			rs=pstm.executeQuery();
                        Customer customer = new Customer();
			while(rs.next()){
                                entity.InfoBooking ifb = new entity.InfoBooking();
				ifb.setId(rs.getInt("id"));
				ifb.setId_customer(rs.getInt("id_customer"));                                 
				ifb.setDate(rs.getString("date_rg"));
				ifb.setNumber_room(rs.getInt("number"));
				ifb.setTotal(rs.getInt("total"));
				ifb.setPayment(rs.getInt("payment"));
                                ifb.setCheckin(rs.getString("date_checkin"));
                                ifb.setCheckout(rs.getString("date_checkout"));
                                ifb.setCustomer(customer.getCustomerById(ifb.getId_customer()));
				list.add(ifb);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		return list;
	}
        public entity.InfoBooking getInfoBookingByCustomer(int id){
		connect= Connector.getConnect();
		ResultSet rs=null;
                 entity.InfoBooking info=null;
		try{

			pstm= connect.prepareStatement("Select *from "+this.table+" where id_customer=?");
                        pstm.setInt(1, id);
			rs=pstm.executeQuery();
                       
			if(rs.next()){
                                info= new entity.InfoBooking();
				info.setId(rs.getInt("id"));                                 
				info.setDate(rs.getString("date_rg"));
				info.setNumber_room(rs.getInt("number"));
				info.setTotal(rs.getInt("total"));
				info.setPayment(rs.getInt("payment"));
                                info.setCheckin(rs.getString("date_checkin"));
                                info.setCheckout(rs.getString("date_checkout"));
				info.setStatus(rs.getInt("status")); 
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		return info;
	}
}
